﻿using DryIoc;
using IntoTheVoid.Units;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntoTheVoid.Models
{
	public class DownLoadInfo {
		private string unopenedLatest;			// 预下载link
		private string unopenedLink;			// 预下载链接
		private string openedLatest;			// 下载version
		private string openedLink;				// 下载link
		private string launcherLatest;			// 启动器version
		private string launcherLink;			// 启动器link
		private bool launcherForceUpdate;		// 启动器强制更新
		private string launcherMD5;				// 启动器md5
		private bool underMaintenance = false;  // 是否维护中
		private string maintenanceTip = "";     // 维护公告

		// 新下载链接逻辑
		private string[] downloadHosts;			// 链接host
		private string openedPath;				// 传的是host的链接路径
		private string unopenedPath;			// 预下载

		
		public string UnopenedLatest { get => unopenedLatest; set => unopenedLatest = value; }
		public string UnopenedLink { get => unopenedLink; set => unopenedLink = value; }
		public string OpenedLatest { get => openedLatest; set => openedLatest = value; }
		public string OpenedLink { get => openedLink; set => openedLink = value; }
		public string LauncherLatest { get => launcherLatest; set => launcherLatest = value; }
		public string LauncherLink { get => launcherLink; set => launcherLink = value; }
		public bool LauncherForceUpdate { get => launcherForceUpdate; set => launcherForceUpdate = value; }
		public string LauncherMD5 { get => launcherMD5; set => launcherMD5 = value; }

		public bool UnderMaintenance { get => underMaintenance; set => underMaintenance = value; }
		public string MaintenanceTip { get => maintenanceTip; set => maintenanceTip = value; }

		public string[] DownloadHosts { get => downloadHosts; set => downloadHosts = value; }
		public string OpenedPath { get => openedPath; set => openedPath = value; }
		public string UnopenedPath { get => unopenedPath; set => unopenedPath = value; }
	}

	public class DownLoadConfigMessage
	{
		private int code;

		private DownLoadInfo data;
		private string error;

		public int Code { get => code; set => code = value; }
		public DownLoadInfo Data { get => data; set => data = value; }
		public string Error { get => error; set => error = value; }
	}
    class GameDownloadConfigModel : SingletonBase<GameDownloadConfigModel>
	{

		private DownLoadInfo downLoadInfo;

		public DownLoadInfo DownLoadInfo { get => downLoadInfo; set => downLoadInfo = value; }

		public event EventHandler<GameDownloadConfigEventArgs> DownLoadConfigCompleted;


		
		public void SendStartConfigMessage(string version = "")
        {
			var channel = INIConfigManagerModel.Instance.getLauncherValueByKey("channel");
			var url = INIConfigManagerModel.Instance.getLauncherValueByKey("down_url");
			Dictionary<string,object> sendDict = new Dictionary<string,object>();
			LogHelper.Info("Ask Url: " + url + "channel: " + channel + "version: " + version);						

			sendDict.Add("channel", channel);
			sendDict.Add("version", version);
			Action<DownLoadConfigMessage> startConfigCallBack = StartConfigCallBack;
			HttpHelper.Instance?.HttpPostHandle(url, sendDict, onExcption: (ex) => {
				DownLoadConfigCompleted.Invoke(this, new GameDownloadConfigEventArgs(-1, ex.ToString()));
			}, startConfigCallBack);
        }

        private void StartConfigCallBack(DownLoadConfigMessage message)
        {
			int code = message?.Code ?? -1;

			if (code == 0)
            {
                DownLoadInfo = message.Data; 
            }

			if (DownLoadConfigCompleted != null)
			{
				DownLoadConfigCompleted.Invoke(this, new GameDownloadConfigEventArgs(code));
			}
        }
    }
}
